#include "edui.h"
using namespace edui;

class HandleEvent : public EventHandler
{
public:
    void onButtonCloseClick(Button* btn) {
        IWindow* wnd = btn->GetWindow();
        wnd->Close();
    }
    
    void onButtonMinClick(Button* btn) {
        IWindow* wnd = btn->GetWindow();
        wnd->MinWindow();
    }
    
    void onButtonMaxClick(Button* btn) {
        IWindow* wnd = btn->GetWindow();
        wnd->MaxWindow();
    }
    
    void onButtonRestoreClick(Button* btn) {
        IWindow* wnd = btn->GetWindow();
        wnd->RestoreWindow();
    }

    //---------------------------//
    void handleMsg(Msg *msg, bool *bHandled) {
        WndMsg *wndmsg = (WndMsg*)msg;
        switch (wndmsg->msg) {
            case MSG_WND_MAXIMIZED: {
                auto max = wndmsg->wnd->FindControl("max");
                auto restore = wndmsg->wnd->FindControl("restore");
                max->SetVisible(false);
                restore->SetVisible(true);
            } break;
            case MSG_WND_RESTORED: {
                auto max = wndmsg->wnd->FindControl("max");
                auto restore = wndmsg->wnd->FindControl("restore");
                max->SetVisible(true);
                restore->SetVisible(false);
            } break;
        }
    }
};

void Init(IWindow *wnd, HandleEvent &evt) {
    wnd->evt.handleMsg.connect(&evt, &HandleEvent::handleMsg);

    Button* button = Button::ToButton(wnd->FindControl("min"));
    if (button){
        button->evt.click.connect(&evt, &HandleEvent::onButtonMinClick);
    }

    button = Button::ToButton(wnd->FindControl("max"));
    if (button){
        button->evt.click.connect(&evt, &HandleEvent::onButtonMaxClick);
    }

    button = Button::ToButton(wnd->FindControl("restore"));
    if (button){
        button->evt.click.connect(&evt, &HandleEvent::onButtonRestoreClick);
    }

    button = Button::ToButton(wnd->FindControl("close"));
    if (button){
        button->evt.click.connect(&evt, &HandleEvent::onButtonCloseClick);
    }
    //---------------------------//
    auto wndctrl = WndCtrl::ToWndCtrl(wnd->FindControl("wndctrl"));
    if (wndctrl) {
        wndctrl->SetWindow(nullptr);
    }
}

int main(int argc, char *argv[])
{
    IEnginePtr->Init();

    IEnginePtr->SetStringGroup("English");
    IEnginePtr->SetXmlDir("test/test4/xml/");

    if (!IEnginePtr->LoadRes("res/res.xml")){
        Log("[main] : load res error.");
    } else {
        IWindow* wnd = IEnginePtr->GetWindowInterface();
        bool bOK = wnd->Create("main", "main", WINDOWPOS_CENTERED, WINDOWPOS_CENTERED, 640, 480, WINDOW_BORDERLESS);
        if (!bOK){
            wnd->Release();
            Log("[main] : create window error.");
        } else if (!wnd->LoadLayout("wnd/main.xml")){
            Log("[main] : load window layout error.");
        } else {

            HandleEvent evt;
            Init(wnd, evt);

            wnd->ShowWindow();

            IEnginePtr->RunMsgLoop(-1);
        }
    }

    IEnginePtr->Quit();
    return 0;
}